package com.enqualcomm.kids.config.userdefault;


import android.content.Context;

import com.enqualcomm.kids.bean.User;
import com.enqualcomm.kids.component.MyApplication;
import com.enqualcomm.kids.network.socket.response.QueryChannelConfigResult;
import com.google.gson.reflect.TypeToken;

import java.util.List;
import java.util.Locale;

import common.utils.BufferedSharedPreferences;
import common.utils.GsonFactory;

/**
 * Created by yinzhiqun on 2015/8/25.
 */
public class AppDefault {
    private static final String APP_CONFIG = "app_config";
    private final BufferedSharedPreferences sp;

    public AppDefault() {
        sp = MyApplication.getInstance().getSp(APP_CONFIG);
    }

    public AppDefault(MyApplication application) {
        sp = application.getSp(APP_CONFIG);
    }

    /**
     * 获取本地保存的图片版本号
     *
     * @return
     */
    public int getGuidePicVersion() {
        return sp.getInt(Keys.App.GUIDE_PIC_VERSION, 0);
    }

    /**
     * 保存看过的引导图片版本号
     *
     * @param version
     */
    public void setGuidePicVersion(int version) {
        sp.saveInt(Keys.App.GUIDE_PIC_VERSION, version);
    }


    /**
     * 保存用户id,如果给null，则清除保存的userid
     */
    public void setUserid(String userid) {
        CacheEntry.set(Keys.App.USER_ID, userid);

        if (userid != null) {
            sp.saveString(Keys.App.USER_ID, userid);
        } else {
            sp.remove(Keys.App.USER_ID);
        }
    }

    /**
     * 获取当前登陆的用户id
     */
    public String getUserid() {
        String id = (String) CacheEntry.get(Keys.App.USER_ID);
        if (id == null) {
            id = sp.getString(Keys.App.USER_ID, null);
            CacheEntry.set(Keys.App.USER_ID, id);
        }
        return id;
    }

    public void setUserkey(String userkey) {
        CacheEntry.set(Keys.App.USER_KEY, userkey);

        if (userkey != null) {
            sp.saveString(Keys.App.USER_KEY, userkey);
        } else {
            sp.remove(Keys.App.USER_KEY);
        }
    }

    public String getUserkey() {
        String key = (String) CacheEntry.get(Keys.App.USER_KEY);
        if (key == null) {
            key = sp.getString(Keys.App.USER_KEY, null);
            CacheEntry.set(Keys.App.USER_KEY, key);
        }
        return key;
    }


    public String getUsername() {
        String str = (String) CacheEntry.get(Keys.App.USERNAME);
        if (str == null) {
            str = sp.getString(Keys.App.USERNAME, null);
            CacheEntry.set(Keys.App.USERNAME, str);
        }
        return str;
    }

    public void setUsername(String username) {
        CacheEntry.set(Keys.App.USERNAME, username);

        if (username != null) {
            sp.saveString(Keys.App.USERNAME, username);
        } else {
            sp.remove(Keys.App.USERNAME);
        }
    }

    public String getPassword() {
        String str = (String) CacheEntry.get(Keys.App.PASSWORD);
        if (str == null) {
            str = sp.getString(Keys.App.PASSWORD, null);
            CacheEntry.set(Keys.App.PASSWORD, str);
        }
        return str;
    }

    public void setPassword(String password) {
        CacheEntry.set(Keys.App.PASSWORD, password);

        if (password != null) {
            sp.saveString(Keys.App.PASSWORD, password);
        } else {
            sp.remove(Keys.App.PASSWORD);
        }
    }

    public String getCountry() {
        String str = (String) CacheEntry.get(Keys.App.COUNTRY);
        if (str == null) {
            str = sp.getString(Keys.App.COUNTRY, null);
            CacheEntry.set(Keys.App.COUNTRY, str);
        }
        return str;
    }

    public void setCountry(String country) {
        CacheEntry.set(Keys.App.COUNTRY, country);

        if (country != null) {
            sp.saveString(Keys.App.COUNTRY, country);
        } else {
            sp.remove(Keys.App.COUNTRY);
        }
    }

    public String getProducts() {
        String str = (String) CacheEntry.get(Keys.App.PRODUCTS);
        if (str == null) {
            str = sp.getString(Keys.App.PRODUCTS, null);
            CacheEntry.set(Keys.App.PRODUCTS, str);
        }
        return str;
    }

    public void setProducts(String products) {
        CacheEntry.set(Keys.App.PRODUCTS, products);

        if (products != null) {
            sp.saveString(Keys.App.PRODUCTS, products);
        } else {
            sp.remove(Keys.App.PRODUCTS);
        }
    }


    public String getPushClientId() {
        String str = (String) CacheEntry.get(Keys.App.PUSH_CLIENT_ID);
        if (str == null) {
            str = sp.getString(Keys.App.PUSH_CLIENT_ID, null);
            CacheEntry.set(Keys.App.PUSH_CLIENT_ID, str);
        }
        return str;
    }

    public void setPushClientId(String clientId) {
        CacheEntry.set(Keys.App.PUSH_CLIENT_ID, clientId);

        if (clientId != null) {
            sp.saveString(Keys.App.PUSH_CLIENT_ID, clientId);
        } else {
            sp.remove(Keys.App.PUSH_CLIENT_ID);
        }
    }


    public String getNotifycationPref() {
        String str = (String) CacheEntry.get(Keys.App.NOTIFYCATION_SETTING);
        if (str == null) {
            str = sp.getString(Keys.App.NOTIFYCATION_SETTING, "1");//默认给1
            CacheEntry.set(Keys.App.NOTIFYCATION_SETTING, str);
        }
        return str;
    }

    public void setNotifycationPref(String setting) {
        CacheEntry.set(Keys.App.NOTIFYCATION_SETTING, setting);

        if (setting != null) {
            sp.saveString(Keys.App.NOTIFYCATION_SETTING, setting);
        } else {
            sp.remove(Keys.App.NOTIFYCATION_SETTING);
        }
    }

    public String getLatLng() {
        String str = (String) CacheEntry.get(Keys.App.LATLNG);
        if (str == null) {
            str = sp.getString(Keys.App.LATLNG, null);
            CacheEntry.set(Keys.App.LATLNG, str);
        }
        return str;
    }

    public void setLatLng(String latLng) {
        CacheEntry.set(Keys.App.LATLNG, latLng);

        if (latLng != null) {
            sp.saveString(Keys.App.LATLNG, latLng);
        } else {
            sp.remove(Keys.App.LATLNG);
        }
    }

    public String getPhoneCityName() {
        String str = (String) CacheEntry.get(Keys.App.PHONE_CITY_NAME);
        if (str == null) {
            str = sp.getString(Keys.App.PHONE_CITY_NAME, null);
            CacheEntry.set(Keys.App.PHONE_CITY_NAME, str);
        }
        return str;
    }

    public void setPhoneCityName(String phoneCityName) {
        CacheEntry.set(Keys.App.PHONE_CITY_NAME, phoneCityName);

        if (phoneCityName != null) {
            sp.saveString(Keys.App.PHONE_CITY_NAME, phoneCityName);
        } else {
            sp.remove(Keys.App.PHONE_CITY_NAME);
        }
    }

    public String getPhoneLatLng() {
        String str = (String) CacheEntry.get(Keys.App.PHONE_LATLNG);
        if (str == null) {
            str = sp.getString(Keys.App.PHONE_LATLNG, null);
            CacheEntry.set(Keys.App.PHONE_LATLNG, str);
        }
        return str;
    }

    public void setPhoneLatLng(String latLng) {
        CacheEntry.set(Keys.App.PHONE_LATLNG, latLng);

        if (latLng != null) {
            sp.saveString(Keys.App.PHONE_LATLNG, latLng);
        } else {
            sp.remove(Keys.App.PHONE_LATLNG);
        }
    }

    public void setChannelConfig(QueryChannelConfigResult config) {
        CacheEntry.set(Keys.App.CHANNEL_CONFIG, config);
        if (config == null) {
            String s = GsonFactory.newInstance().toJson(config);
            sp.saveString(Keys.App.CHANNEL_CONFIG, s);
        } else {
            sp.remove(Keys.App.CHANNEL_CONFIG);
        }
    }


    public QueryChannelConfigResult.Result getChannelConfig() {
        QueryChannelConfigResult config = (QueryChannelConfigResult) CacheEntry.get(Keys.App.CHANNEL_CONFIG);
        if (config != null) {
            return config.result;
        } else {
            String s = sp.getString(Keys.App.CHANNEL_CONFIG, null);
            if (s != null) {
                config = GsonFactory.newInstance().fromJson(s, QueryChannelConfigResult.class);
                CacheEntry.set(Keys.App.CHANNEL_CONFIG, config);
                return config.result;
            }
            return null;
        }
    }

    /**
     * 获取登录输入框记住的账号密码
     *
     * @return
     */
    public List<User> getUsers() {
        String str = sp.getString(Keys.App.USERS, null);
        if (str != null) {
            return GsonFactory.newInstance().fromJson(str, new TypeToken<List<User>>() {
            }.getType());
        }
        return null;
    }

    /**
     * 保存账号密码
     */
    public void setUsers(List<User> users) {
        if (users != null && users.size() != 0) {
            String str = GsonFactory.newInstance().toJson(users);
            sp.saveString(Keys.App.USERS, str);
        } else {
            sp.remove(Keys.App.USERS);
        }
    }

    /**
     * 清除所有数据
     */
    public void clear() {
        sp.clear();
    }


    public static String getDefaultIconPath(Context context) {
        return context.getFilesDir() + "/default_icon.png";
    }

    public static String getGirlIconPath(Context context) {
        return context.getFilesDir() + "/default_icon_girl.png";
    }

    public static String getBoyIconPath(Context context) {
        return context.getFilesDir() + "/default_icon_boy.png";
    }

    public static String getDefaultIconPathOldMan(Context context) {
        return context.getFilesDir() + "/default_icon_old.png";
    }

    public static String getGirlIconPathOldMan(Context context) {
        return context.getFilesDir() + "/default_icon_girl_old.png";
    }

    public static String getBoyIconPathOldMan(Context context) {
        return context.getFilesDir() + "/default_icon_boy_old.png";
    }


//    public boolean isOldManMode(){
//        String runningMode = getRunningMode();
//        return OLD_MAN_MODE.equals(runningMode);
//
//    }

//    public boolean isPetMode(){
//        String runningMode = getRunningMode();
//        return PET_MODE.equals(runningMode);
//    }

    public static boolean isOldManCid(String imei) {
        return imei.length() == 16 && imei.endsWith("1");
    }

//    public static boolean isPetCid(String imei){
//        return imei.length()==16 && imei.toUpperCase(Locale.getDefault()).endsWith("A");
//    }

//    public static String OLD_MAN_MODE = "oldman";
//    public static String DEFAULT_MODE = "child";
//    public static String PET_MODE = "pet";

//    public void setRunningMode(String imei){
//        String mode = DEFAULT_MODE;
//        if (isOldManCid(imei)){
//            mode = OLD_MAN_MODE;
//        }else if (isPetCid(imei)){
//            mode = PET_MODE;
//        }
//
//        CacheEntry.set(Keys.App.RUNNING_MODE,mode);
//
//        if (mode!=null){
//            sp.saveString(Keys.App.RUNNING_MODE,mode);
//        }else{
//            sp.remove(Keys.App.RUNNING_MODE);
//        }
//    }

//    private String getRunningMode(){
//        String str = (String)CacheEntry.get(Keys.App.RUNNING_MODE);
//        if (str==null){
//            str = sp.getString(Keys.App.RUNNING_MODE, null);
//            CacheEntry.set(Keys.App.RUNNING_MODE, str);
//        }
//        return str;
//    }

    public int getWindowWidth() {
        return sp.getInt(Keys.App.WINDOW_WIDTH, 720);
    }

    public void setWindowWidth(int width) {
        sp.saveInt(Keys.App.WINDOW_WIDTH, width);
    }

    public int getWindowHeight() {
        return sp.getInt(Keys.App.WINDOW_HEIGHT, 1280);
    }

    public void setWindowHeight(int height) {
        sp.saveInt(Keys.App.WINDOW_HEIGHT, height);
    }

    /**
     * 添加第三方微信登陆使用
     * 保存用户微信id,如果给null，则清除保存的userid
     */
    public void setWXUserid(String userid) {
        CacheEntry.set(Keys.App.WX_USER_ID, userid);

        if (userid != null) {
            sp.saveString(Keys.App.WX_USER_ID, userid);
        } else {
            sp.remove(Keys.App.WX_USER_ID);
        }
    }

    /**
     * 添加第三方微信登陆使用
     * 获取当前登陆的用户微信id
     */
    public String getWXUserid() {
        String id = (String) CacheEntry.get(Keys.App.WX_USER_ID);
        if (id == null) {
            id = sp.getString(Keys.App.WX_USER_ID, null);
            CacheEntry.set(Keys.App.WX_USER_ID, id);
        }
        return id;
    }


    public void setWXUserNickName(String name) {
        CacheEntry.set(Keys.App.WX_USER_NICK_NAME, name);

        if (name != null) {
            sp.saveString(Keys.App.WX_USER_NICK_NAME, name);
        } else {
            sp.remove(Keys.App.WX_USER_NICK_NAME);
        }
    }

    /**
     * 添加第三方微信登陆使用
     * 获取当前登陆的用户微信昵称
     */
    public String getWXUserNickName() {
        String id = (String) CacheEntry.get(Keys.App.WX_USER_NICK_NAME);
        if (id == null) {
            id = sp.getString(Keys.App.WX_USER_NICK_NAME, null);
            CacheEntry.set(Keys.App.WX_USER_NICK_NAME, id);
        }
        return id;
    }

    /**
     * 判断APP是否是微信登陆
     */
    public void setLoginByWX(boolean isWX) {
        CacheEntry.set(Keys.App.WX_LOGIN, isWX);
        sp.saveBoolean(Keys.App.WX_LOGIN, isWX);
    }

    public boolean getLoginByWX() {
        boolean isLoginByWX = false;
        isLoginByWX = sp.getBoolean(Keys.App.WX_LOGIN, false);
        CacheEntry.set(Keys.App.WX_LOGIN, isLoginByWX);
        return isLoginByWX;
    }

    /**
     * 添加第三方微信登陆使用
     * 获取当前登陆的用户微信用户头像地址
     */
    public void setWXUserHeadUrl(String headerUrl) {
        CacheEntry.set(Keys.App.WX_USER_HEAD_URL, headerUrl);

        if (headerUrl != null) {
            sp.saveString(Keys.App.WX_USER_HEAD_URL, headerUrl);
        } else {
            sp.remove(Keys.App.WX_USER_HEAD_URL);
        }
    }


    public String getWXUserHeadUrl() {
        String id = (String) CacheEntry.get(Keys.App.WX_USER_HEAD_URL);
        if (id == null) {
            id = sp.getString(Keys.App.WX_USER_HEAD_URL, null);
            CacheEntry.set(Keys.App.WX_USER_HEAD_URL, id);
        }
        return id;
    }

    /**
     * 添加第三方微信登陆使用
     * 获取当前登陆的用户微信登陆时的类型
     * 0表示未在系统中注册，1表示已关联绑定了账号，登录返回绑定的账号，2表示已关联手表,3未关联手表，判断是否有手机号码
     */
    public void setWXLoginType(int type) {
        sp.saveInt(Keys.App.WX_LOGIN_TYPE, type);
    }

    public int getWXLoginType() {
        return sp.getInt(Keys.App.WX_LOGIN_TYPE, 500);
    }
    public String getDisplayUsername(){
        return sp.getString(Keys.App.DISPLAY_USERNAME,null);
    }

    public void setDisplayUsername(String displayUsername){
        sp.saveString(Keys.App.DISPLAY_USERNAME,displayUsername);
    }


    public String getBbtreeUserid(){
        return sp.getString(Keys.App.BBTREE_UID,null);
    }

    public void setBbtreeUserid(String bbtreeUserid){
        sp.saveString(Keys.App.BBTREE_UID,bbtreeUserid);
    }

    public String getBbtreeUserPhoneNumber(){
        return sp.getString(Keys.App.BBTREE_PHONE,null);
    }

    public void setBbtreeUserPhoneNumber(String phoneNumber){
        sp.saveString(Keys.App.BBTREE_PHONE,phoneNumber);
    }




    public String getApiUsername(){
//        return sp.getString(Keys.App.API_USERNAME,null);//如果从老版本升级，会有问题，自动登录时没有调用login
        String username = sp.getString(Keys.App.API_USERNAME, null);
        if (username!=null){
            return username;
        }else {
            username = getUsername();
            String country = getCountry();
            return "00"+country+username;
        }
    }

    public void setApiUsername(String apiUsername){
        sp.saveString(Keys.App.API_USERNAME,apiUsername);
    }
}
